package cn.kgc.mybatis.web.utils;

import cn.hutool.json.JSONUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 课工场
 * @date 2024/10/31
 * @description
 */
public class ResponseUtil {


    public static void  writeJson(HttpServletResponse response ,Object data){

        // 设置响应格式
        response.setContentType("application/json;charset=utf-8");
        try {
            PrintWriter writer = response.getWriter();
            //  java  object   ->  String  json字符串
            String jsonStr = JSONUtil.toJsonStr(data);
            writer.write(jsonStr);
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
